﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using CourseArraging.Common;
using System.IO;
using System.Text.RegularExpressions;
using Stone.DataContracts;
using System.Configuration;
using Stone.Library.Common;

namespace CourseArraging
{
    public partial class FrmLogin : Form
    {
        #region 字段属性
        YhxxService.YhxxServiceClient yhxxdao = new YhxxService.YhxxServiceClient();
        string key = ConfigurationManager.AppSettings["IpPort"].ToString();
        #endregion

        #region 构造器
        public FrmLogin()
        {
            InitializeComponent();
            txt_address.Text = key;
        }
        #endregion

        #region 事件
        private void btn_load_Click(object sender, EventArgs e)
        {
            #region 过期监测
            string strTime = string.Empty;
            string strDate = string.Empty;
            try
            {
                GetNistTimeCN(out strTime, out strDate);
                DateTime dt = DateTime.Parse(strDate);
                if (dt > new DateTime(2011, 6, 21))
                {
                    MessageBox.Show("该软件已过期");
                    return;
                }
            }
            catch
            {
                this.Close();
            }
            #endregion
            #region 地址更改
            string newIp=this.txt_address.Text.Trim();
            if (!newIp.Equals(key))  
            {
                ConfigHelp.setAppValue("IpPort", newIp);
                ConfigHelp.ChanageConfig(newIp);
                MessageBox.Show("服务器地址更改成功，请重新启动！");
                this.Close();
            }
            #endregion
            if (txt_name.Text == "admin")
            {
                GlobalVar.yhEntity = new TecEntity();
                FrmMain frmMain = new FrmMain();
                frmMain.Show();
                this.Hide();
                return;
            }
            //TecEntity tecEntity = yhxxdao.GetYhxxEntity8Condi(null, txt_name.Text.Trim());
            //if (tecEntity == null)
            //{
            //    MessageBox.Show("该用户不存在! 请重新输入用户名");
            //    return;
            //}
            //if (tecEntity.user_psw == txt_psw.Text.Trim())
            //{
            //    GlobalVar.yhEntity = tecEntity;
            //    FrmMain frmMain = new FrmMain();
            //    frmMain.Show();
            //    this.Hide();
            //}
            //else
            //{
            //    MessageBox.Show("密码错误! 请核实后重新输入!");
            //    return;
            //}
        }
        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 方法

        //获取中国标准时间（由美国官方时间折算）
        private void GetNistTimeCN(out string time, out string Data)
        {
            try
            {
                //nist.time.gov的url
                string strNistUrl = "http://nist.time.gov/timezone.cgi?UTC/s/0";

                //构造并实例化一个WebRequest
                System.Net.WebRequest myHttpWebRequest = System.Net.HttpWebRequest.Create(strNistUrl);
                //设置连接超时时间
                myHttpWebRequest.Timeout = 8000;
                //设置WebResponse，接收返回信息
                System.Net.WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse();
                //获取返回信息流信息
                Stream sr = myHttpWebResponse.GetResponseStream();
                //设置流阅读器
                StreamReader reader = new StreamReader(sr, System.Text.Encoding.ASCII);
                //流输出为字符串
                String srdata = reader.ReadToEnd();
                //将返回html文本中的双引号变为单引号
                srdata = srdata.Replace("\"", "\'");

                //用模式匹配加分割的方式定位和获取时间信息
                string strOut = Regex.Split(srdata, "color='white'><b>", RegexOptions.IgnoreCase)[1];
                string strTime = Regex.Split(strOut, "<br>", RegexOptions.IgnoreCase)[0];
                //小时需调整，因时区相差8小时
                time = DateTime.Parse(strTime).AddHours(8).ToLongTimeString();

                //用模式匹配加分割的方式定位和获取日期信息
                string strOut1 = Regex.Split(srdata, "'5' color='white'>", RegexOptions.IgnoreCase)[1];
                string strData = Regex.Split(strOut1, "<br>", RegexOptions.IgnoreCase)[0];
                //获取中国时间-小时部分
                int intHourCn = int.Parse(time.Split(':')[0]);
                //获取美国时间-小时部分
                int intHourUS = int.Parse(DateTime.Parse(strTime).ToLongTimeString().Split(':')[0]);
                //比较小时差异，调整日期差异
                if (intHourCn < intHourUS)
                {
                    Data = DateTime.Parse(strData).AddDays(1).ToShortDateString();
                }
                else
                {
                    Data = DateTime.Parse(strData).ToShortDateString();
                }
            }
            catch (System.Exception ex)
            {
                time = string.Empty;
                Data = string.Empty;
                MessageBox.Show("无法连接网络：" + ex.Message,"连接失败");
            }
        }

        #endregion
    }
}
